package com.qiaoyanqing.ximalaya.downloadmodule.download_fragment;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioGroup;

import com.qiaoyanqing.ximalaya.R;

import java.util.ArrayList;
import java.util.List;

public class HotRecommendActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener,View.OnClickListener{
    //头部导航
    private RadioGroup radioGroup;
    private ViewPager pager;
    //碎片
    private HotFragment hotFragment;
    private NewFragment newFragment;
    private ClassicFragment classicFragment;
    //碎片集合
    private List<Fragment> fragmentList = new ArrayList<>();
    //左上返回箭头
    private ImageView back_img;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hot_recommend);

        initView();

        initData();
    }

    private void initData() {
        FragmentAdapter adapter = new FragmentAdapter(getSupportFragmentManager(),fragmentList);
        pager.setAdapter(adapter);
        pager.setCurrentItem(0);
        radioGroup.setOnCheckedChangeListener(this);
    }

    //初始化控件
    private void initView() {
        radioGroup = (RadioGroup) findViewById(R.id.radio_group);
        pager = (ViewPager) findViewById(R.id.view_pager);
        back_img = (ImageView) findViewById(R.id.backMine);
        back_img.setOnClickListener(this);

        hotFragment = new HotFragment();
        newFragment = new NewFragment();
        classicFragment = new ClassicFragment();

        fragmentList.add(hotFragment);
        fragmentList.add(newFragment);
        fragmentList.add(classicFragment);
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        for (int i1 = 0; i1 < radioGroup.getChildCount(); i1++) {
            if (radioGroup.getChildAt(i1).getId()==i){
                pager.setCurrentItem(i1);
            }
        }
    }
    //左上返回箭头
    @Override
    public void onClick(View view) {
        finish();
    }
}
